package com.godpaper.twoHitOne.busniess.managers
{
	import com.godpaper.twoHitOne.model.BoardModel;
	import com.godpaper.twoHitOne.model.PiecesModel;
	import com.godpaper.twoHitOne.model.PlayerModel;
	import com.godpaper.twoHitOne.util.PositionTransactUtil;
	import com.godpaper.twoHitOne.views.components.GasketButton;
	import com.godpaper.twoHitOne.views.components.PieceButton;
	
	import de.polygonal.ds.Array2;
	
	import flash.geom.Point;
	
	public class PiecesManager
	{
		public function PiecesManager()
		{
		}
		
		public static function tryMovement(target:PieceButton,destination:Point,position:Array):Boolean
		{
			//			trace("target name: ",target.pieceVO.name,"destination original point: ",destination.x,destination.y);
			//refresh gamePosition
			trace("target name@tryMovement:",BoardModel.getInstance().gamePosition.gett(target.pieceVO.position[0],target.pieceVO.position[1]).pieceVO.name);
			trace("target original position@tryMovement:",target.pieceVO.position[0],target.pieceVO.position[1]);
			BoardModel.getInstance().gamePosition.sett(target.pieceVO.position[0],target.pieceVO.position[1],null);
			trace("before movement@tryMovement:",BoardModel.getInstance().gamePosition.dump());
			
			//cover protected position
			/*trace("BoardModel.getInstance().protectedFields.gett(target.pieceVO.position[0],target.pieceVO.position[1]:",
			BoardModel.getInstance().protectedFields.gett(target.pieceVO.position[0],target.pieceVO.position[1]));
			if( (BoardModel.getInstance().protectedFields.gett(target.pieceVO.position[0],target.pieceVO.position[1]))!=null  )
			{
			BoardManager.cleanProtectedFields();
			}*/
			
			//target movement;
			target.move( destination.x,destination.y);
			//			trace("target new point: ",Application.application.localToGlobal(new Point(target.x,target.y)).toString());
			trace("target new position@tryMovement:",position.toString());
			
			//refresh new position;
			if(PiecesModel.getInstance().selectedPiece.pieceVO.name.indexOf(PieceButton.NAME_PREFIX_RED)!=-1)
			{
				(PiecesModel.getInstance().redPiecesCollection.getItemAt(PiecesModel.getInstance().redPiecesCollection.getItemIndex(target)) as PieceButton).pieceVO.position = position;
			}else
			{
				(PiecesModel.getInstance().bluePiecesCollection.getItemAt(PiecesModel.getInstance().bluePiecesCollection.getItemIndex(target)) as PieceButton).pieceVO.position = position;
			}
			
			//refresh gamePosition
			BoardModel.getInstance().gamePosition.sett(position[0],position[1],target);
			trace("after arrived@tryMovement:",BoardModel.getInstance().gamePosition.dump());
			
			//calculateResult:
			calculateResult(BoardModel.getInstance().gamePosition);
			
			//discover protected position
			/*for(var i:int=0;i<BoardModel.getInstance().gamePosition.width;i++)
			{
			BoardManager.discoverHorizontalProtectedFields(BoardModel.getInstance().gamePosition.getCol(i));
			}
			for(var j:int=0;j<BoardModel.getInstance().gamePosition.height;j++)
			{
			BoardManager.discoverVerticalProtectedFields(BoardModel.getInstance().gamePosition.getRow(j));
			}*/
			
			
			
			return true;
		}
		
		//simulation pieceButtons such as ToggleButtonBar;
		public function untoggleOtherPieces(nameFilter:String):void
		{
			var myPieceButton:PieceButton; 
			for(var i:int=0;i<PiecesModel.getInstance().bluePiecesCollection.length;i++)
			{
				myPieceButton = PiecesModel.getInstance().bluePiecesCollection.getItemAt(i) as PieceButton; 
				if(myPieceButton.pieceVO.name!=nameFilter)
				{
					myPieceButton.selected = false;
				}
			}
		}
		
		public function untoggleOtherGaskets(position:Array):void
		{
			//untoggle other gasketbuttons;
			for(var i:int=0;i<PiecesModel.getInstance().gasketCollection.length;i++)
			{
				(PiecesModel.getInstance().gasketCollection.getItemAt(i)  as GasketButton).enabled = false;
			}
			//toggle this four direction gasketbuttons
			
			//left gasketButton
			if(BoardModel.getInstance().gamePosition.gett((position[0]-1),position[1])==null)
			{
				if((PositionTransactUtil.transactArray2Index(position)-1)%4!=3)//except 3,7,11,15cuz it is a boundary of board.
				{
					try
					{
						trace("untoggled left gasketButton: ",PiecesModel.getInstance().gasketCollection.getItemAt(PositionTransactUtil.transactArray2Index(position)-1));
						(PiecesModel.getInstance().gasketCollection.getItemAt(PositionTransactUtil.transactArray2Index(position)-1) as GasketButton).enabled = true;
					}catch(error:Error){};
				}
			}
			//top gasketButton
			if(BoardModel.getInstance().gamePosition.gett(position[0],position[1]-1)==null)
			{
				try
				{
					trace("untoggled top gasketButton: ",PiecesModel.getInstance().gasketCollection.getItemAt(PositionTransactUtil.transactArray2Index(position)-4));
					(PiecesModel.getInstance().gasketCollection.getItemAt(PositionTransactUtil.transactArray2Index(position)-4) as GasketButton).enabled = true;
				}catch(error:Error){};	
			}	
			//right gasketButton
			if(BoardModel.getInstance().gamePosition.gett(position[0]+1,position[1])==null)
			{
				if((PositionTransactUtil.transactArray2Index(position)+1)%4!=0)//except 0,4,8,12cuz it is a boundary of board.
				{
					try
					{
						trace("untoggled right gasketButton: ",PiecesModel.getInstance().gasketCollection.getItemAt(PositionTransactUtil.transactArray2Index(position)+1));
						(PiecesModel.getInstance().gasketCollection.getItemAt(PositionTransactUtil.transactArray2Index(position)+1)  as GasketButton).enabled = true;
					}catch(error:Error){};	
				}	
			}
			//bottom gasketButton
			if(BoardModel.getInstance().gamePosition.gett(position[0],position[1]+1)==null)
			{
				try
				{
					trace("untoggled bottom gasketButton: ",PiecesModel.getInstance().gasketCollection.getItemAt(PositionTransactUtil.transactArray2Index(position)+4));
					(PiecesModel.getInstance().gasketCollection.getItemAt(PositionTransactUtil.transactArray2Index(position)+4) as GasketButton).enabled = true;
				}catch(error:Error){};
			}	
		}
													
		private static function calculateResult(gamePositon:Array2):void
		{
			if(!BoardManager.analysisPhases(PiecesModel.getInstance().redPiecesCollection,PiecesModel.getInstance().bluePiecesCollection))
			{
				if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
				{
					PlayerManager.isComputerTurnNow();
				}else
				{
					PlayerManager.isHumanTurnNow();	
				}
			}
		}
	}
}